package vip.zhenzicheng.nettybasic.outread;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * 客户端业务的Handler
 *
 * @author zhenzicheng
 * @date 2022-06-07 20:24
 */
public class EchoClientORHandler extends SimpleChannelInboundHandler<ByteBuf> {

  // 读取到网络数据后进行业务处理，并关闭连接
  @Override
  protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
    System.out.println("client accept:" + byteBuf.toString(CharsetUtil.UTF_8));
  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.writeAndFlush(Unpooled.copiedBuffer("Hello,Netty", CharsetUtil.UTF_8));
  }
}
